/**
 * Alipay.com Inc.
 * Copyright (c) 2004-2022 All Rights Reserved.
 */
package com.example.spider.proxy.provider.xsdaili;

import cn.hutool.core.util.ReUtil;
import com.example.spider.proxy.core.ProxyInfo;
import com.example.spider.proxy.core.tester.ProxyTypeEnum;
import com.example.spider.proxy.provider.AbstractProxyResultHandler;
import com.example.spider.proxy.provider.IpProxySpiderTask;
import com.example.spider.starter.SpiderStarter;
import com.example.spider.util.FileBasedCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.ConcurrentSkipListSet;

/**
 * 小舒代理，每天发两篇代理文章，双数国外，单数国内，建议每小时/每天爬一次
 * 2017年05月23日第一天发布，从下标为2开始，每天发两篇，网站响应比较慢
 *
 * @author lym
 */
//@Component
public class XsDailiResultHandler extends AbstractProxyResultHandler implements SpiderStarter {

    @Qualifier("proxy_xsdaili_distinct_set")
    @Autowired
    private FileBasedCollection<ConcurrentSkipListSet<String>, String> distinctSet;

    public XsDailiResultHandler() {
        super("xsDaili");
    }

    @Override
    public void startSpiders() {
//        IpProxySpiderTask task = createTask("http://www.xsdaili.cn/dayProxy/ip/3540.html");
//        SpiderTaskQueue.putTask(task);
    }

    /**
     * 每天 2. 触发一次
     */
    @Override
    public String executeCron() {
        return null;
    }

    protected Duration defaultLimitRateDuration() {
        return Duration.ofMillis(1000);
    }

    /**
     * 处理响应
     */
    @Override
    public void doHandle(IpProxySpiderTask task, String html) {

        // 222.223.182.66:8000@HTTP#[高匿名]河北沧州 电信 <br>
        String regex = "(\\d+\\.\\d+\\.\\d+\\.\\d+):(\\d+)@(\\w+)#\\[(.*?)\\](.*?) (.*?) <br>";

        List<String> ipList = ReUtil.findAll(regex, html, 1);
        List<String> portList = ReUtil.findAll(regex, html, 2);
        List<String> typeList = ReUtil.findAll(regex, html, 3);
        List<String> anonymityList = ReUtil.findAll(regex, html, 4);
        List<String> locationList = ReUtil.findAll(regex, html, 5);
        List<String> operatorList = ReUtil.findAll(regex, html, 6);

        for (int i = 0; i < ipList.size(); i++) {
            ProxyInfo proxyInfo = ProxyInfo.builder()
                    .source(taskName)
                    .ip(ipList.get(i))
                    .port(Integer.parseInt(portList.get(i)))
                    .operator(operatorList.get(i))
                    .proxyTypeEnum(ProxyTypeEnum.of(typeList.get(i)))
                    .type(ProxyTypeEnum.of(typeList.get(i)).getType())
                    .anonymity(anonymityList.get(i))
                    .location(locationList.get(i))
                    .build();
            // 保存
            addProxyToSourceRepository(proxyInfo);
        }
        distinctSet.add(task.getUrl().split("www.xsdaili.cn")[1]);
    }

}